<?php

class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library("BaoMat");
        if ($this->session->userdata("logged_in") != true) {
            header("location:./");
        }
        $this->load->Model("Madmin");
    }

    function index() {

        $data["username"] = $this->session->userdata('username');
        $this->load->view(__TEMPLATES_PATH__ . "/admin/index", $data);
    }
public function gettintuc() {

        $tintuc = $this->Madmin->gettintuc();
        $jsonstring = json_encode($tintuc);
        echo $jsonstring;
    }
    public function getchuyenmuc() {

        $menu = $this->Madmin->getmenu();
        $jsonstring = json_encode($menu);
        echo $jsonstring;
    }
    public function  getsubchuyenmuc(){
             $submenu = $this->Madmin->getsubmenu();
        $jsonstring = json_encode($submenu);
        echo $jsonstring;
    }
    public function xoasubchuyenmuc($idsubchuyenmuc){
        $resuilt = $this->Madmin->xoasubchuyenmuc($idsubchuyenmuc);
        $msg = Array();
        if ($resuilt == "1") {
            $msg = "Xóa thành công";
        } else {
            $msg = "Xóa thất bại";
        }
        $jsonstring = json_encode($msg);
        echo $jsonstring;
    }

    public function xoachuyenmuc($idchuyenmuc) {
        $resuilt = $this->Madmin->xoachuyenmuc($idchuyenmuc);
        $msg = Array();
        if ($resuilt == "1") {
            $msg = "Xóa thành công";
        } else {
            $msg = "Xóa thất bại";
        }
        $jsonstring = json_encode($msg);
        echo $jsonstring;
    }
    public function getidsuasub($idsua){
              $resuilt = $this->Madmin->getidsuasub($idsua);
         $msg = Array();
        if (count($resuilt) == "0") {
            $msg = "error";
        } else {
            $msg = $resuilt;
        }
        $jsonstring = json_encode($msg);
        echo $jsonstring;
    }

    public function getidsua($idsua) {
        $resuilt = $this->Madmin->getidsua($idsua);
        $msg = Array();
        if (count($resuilt) == "0") {
            $msg = "error";
        } else {
            $msg = $resuilt;
        }
        $jsonstring = json_encode($msg);
        echo $jsonstring;
    }
    
    public function suasubchuyenmuc(){
        $sub_category_name = $this->baomat->cleanXSS($_POST['sub_category_name']);
        $sub_category_desc = $this->baomat->cleanXSS($_POST['sub_category_desc']);
        $opchuyenmuc = $this->baomat->cleanXSS($_POST['opchuyenmuc']);
        $sub_category_links = $this->baomat->cleanXSS($_POST['sub_category_links']);
        $status = $this->baomat->cleanXSS($_POST['status']);
          $id = $this->baomat->cleanXSS($_POST['sub_category_id']);
       $resuilt =  $this->Madmin->suasubchuyenmuc($sub_category_name, $sub_category_desc, $opchuyenmuc, $sub_category_links, $status, $id);
      $msg = array();
       if($resuilt == "1"){
           $msg = "Sửa thành công";
       }else {
           $msg = "Sửa thất bại , vui lòng thử lại sau";
       }
       $jsonstring = json_encode($msg);
       echo $jsonstring;
    }

    public function suachuyenmuc() {
        $category_name = $this->baomat->cleanXSS($_POST['category_name']);
        $category_desc = $this->baomat->cleanXSS($_POST['category_desc']);
        $class = $this->baomat->cleanXSS($_POST['class']);
        $category_links =$this->baomat->cleanXSS($_POST['category_links']);
        $status = $this->baomat->cleanXSS($_POST['status']);
          $id = $this->baomat->cleanXSS($_POST['category_id']);
       $resuilt =  $this->Madmin->suachuyenmuc($category_name, $category_desc, $class, $category_links, $status, $id);
      $msg = array();
       if($resuilt == "1"){
           $msg = "Sửa thành công";
       }else {
           $msg = "Sửa thất bại , vui lòng thử lại sau";
       }
       $jsonstring = json_encode($msg);
       echo $jsonstring;
    }
    public function themsubchuyenmuc(){
        $sub_category_name = $this->baomat->cleanXSS($_POST['sub_category_name']);
        $sub_category_desc = $this->baomat->cleanXSS($_POST['sub_category_desc']);
        $opchuyenmuc = $this->baomat->cleanXSS($_POST['opchuyenmuc']);
        $sub_category_links = $this->baomat->cleanXSS($_POST['sub_category_links']);
        $status = $this->baomat->cleanXSS($_POST['status']);
        $resuilt =  $this->Madmin->themsubchuyenmuc($sub_category_name, $sub_category_desc, $opchuyenmuc, $sub_category_links, $status);
      $msg = array();
       if($resuilt == "1"){
           $msg = "Thêm  thành công";
       }else {
           $msg = "Sửa thất bại , vui lòng thử lại sau";
       }
       $jsonstring = json_encode($msg);
       echo $jsonstring;
    }

    public function themchuyenmuc(){
          $category_name = $this->baomat->cleanXSS($_POST['category_name']);
        $category_desc = $this->baomat->cleanXSS($_POST['category_desc']);
        $class = $this->baomat->cleanXSS($_POST['class']);
        $category_links = $this->baomat->cleanXSS($_POST['category_links']);
        $status = $this->baomat->cleanXSS($_POST['status']);
       $resuilt =  $this->Madmin->themchuyenmuc($category_name, $category_desc, $class, $category_links, $status);
      $msg = array();
       if($resuilt == "1"){
           $msg = "Thêm  thành công";
       }else {
           $msg = "Sửa thất bại , vui lòng thử lại sau";
       }
       $jsonstring = json_encode($msg);
       echo $jsonstring;
    }

}
